#pragma once
#include "data.hpp"
#include <unistd.h>

extern cloud::dataManager *data;

namespace cloud
{
    class hotManager
    {
    private:
        std::string _url_backup;
        std::string _url_pack;
        std::string _suffix_pack;
        size_t _hot_time;

    private:
        bool isHot(const std::string &name)
        {
            FileTool ft(name);
            time_t now = time(nullptr);
            if (now - ft.getATime() > _hot_time)
            {
                return false;
            }
            return true;
        }

    public:
        hotManager()
        {
            _url_backup = config::getInstance()->getUrlBackup();
            _url_pack = config::getInstance()->getUrlPackdir();
            _suffix_pack = config::getInstance()->getSuffixPack();
            _hot_time = config::getInstance()->getHotTime();
            
            // 防止没有这两个目录
            FileTool ft1(_url_backup);
            FileTool ft2(_url_pack);
            ft1.createDirectory();
            ft2.createDirectory();
        }

        void start()
        {
            while (true)
            {
                FileTool ft(_url_backup);
                std::vector<std::string> array;
                ft.scanDirectory(&array);

                for (auto &it : array)
                {
                    // 热点文件跳过
                    if (isHot(it))
                    {
                        continue;
                    }

                    backupData bd;
                    if (data->getOneReal(it, &bd) == false)
                    {
                        bd.setBackupdata(it);
                    }

                    FileTool tmp(it);
                    tmp.zip(bd.url_pack);
                    bd.is_pack = true;

                    tmp.remove();
                    data->updata(bd);
                }
                usleep(1000);
            }
        }
    };
}